#!/bin/bash

# eg: ./sign_oh_lite_app.sh ./haps/testdemo.hap ./haps com.example.testdemo

OUTPUT_HAP_PATH=$2
BUNDLE_NAME=$3
HAP_FILE=$(basename $1)
APP_NAME=${HAP_FILE%.*}
UNSIGNED_APP_NAME="${APP_NAME}_unsigned.bin"
SIGNED_APP_NAME="${APP_NAME}_signed.bin"
TMP_SUFFIX=`mktemp -u XXX`
NEW_HAP_FILE="${APP_NAME}_new.hap"
TMP_DIR="hap_pack"

MOD_PROFILE="releaseProfile.json"
PROFILE_SIGNED_FILE="profile_signed.p7b"

APP_KEY_ALIAS="openharmony application release"
APP_CA_KEY_ALIAS="openharmony application ca"
APP_CA_SUBJECT="C=CN,O=OpenHarmony,OU=OpenHarmony Team,CN= OpenHarmony Application CA"
APP_SUBJECT="C=CN,O=OpenHarmony,OU=OpenHarmony Team,CN=OpenHarmony Application Release"
APP_SUB_CA_FILE="subCA.cer"
ROOT_CA_FILE="rootCA.cer"
OH_KEY_FILE="OpenHarmony.p12"
APP_CERT_FILE="OpenHarmonyApplication.pem"
PROFILE_KEY_ALIAS='openharmony application profile release'
PROFILE_CERT_FILE="OpenHarmonyProfileRelease.pem"

SCRIPT_PATH=$(dirname "$(realpath "$0")")
echo "script path is $SCRIPT_PATH"

SIGN_APP_ROOT_DIR="${SCRIPT_PATH}/sign_app_dir"
SIGN_APP_DIR="${SIGN_APP_ROOT_DIR}/${APP_NAME}_${TMP_SUFFIX}"
CP_HAP_PATH="${SIGN_APP_DIR}/${HAP_FILE}"
PROFILE_TEMPLATE="${SCRIPT_PATH}/UnsgnedReleasedProfileTemplate.json"

if [ ! -d "$SIGN_APP_ROOT_DIR" ]; then
    mkdir -p "$SIGN_APP_ROOT_DIR"
fi

if [ ! -d "$SIGN_APP_DIR" ]; then
    mkdir -p "$SIGN_APP_DIR"
else
    rm -rf "$SIGN_APP_DIR"
    mkdir -p "$SIGN_APP_DIR"
fi

cp $1 ${CP_HAP_PATH}

# unpacking the hap to directory
echo "unpacking ${CP_HAP_PATH} to ${SIGN_APP_DIR}/${TMP_DIR}"
java -jar "${SCRIPT_PATH}/app_unpacking_tool.jar" --mode hap --hap-path "${CP_HAP_PATH}" --out-path "${SIGN_APP_DIR}/${TMP_DIR}"

# packing the directory to .hap
echo "packing ${SIGN_APP_DIR}/${TMP_DIR} to ${SIGN_APP_DIR}/${NEW_HAP_FILE}"
PACK_OTHER_PARAS=""
if [ -e "${SIGN_APP_DIR}/${TMP_DIR}/rpcid.sc" ]; then
    PACK_OTHER_PARAS+="--rpcid-path ${SIGN_APP_DIR}/${TMP_DIR}/rpcid.sc"
fi
if [ -d "${SIGN_APP_DIR}/${TMP_DIR}/libs" ]; then
    PACK_OTHER_PARAS+=" --lib-path ${SIGN_APP_DIR}/${TMP_DIR}/libs"
fi
java -jar "${SCRIPT_PATH}/app_packing_tool.jar" --mode hap --json-path "${SIGN_APP_DIR}/${TMP_DIR}/config.json" --pack-info-path "${SIGN_APP_DIR}/${TMP_DIR}/pack.info" --assets-path "${SIGN_APP_DIR}/${TMP_DIR}/assets" --out-path "${SIGN_APP_DIR}/${NEW_HAP_FILE}" ${PACK_OTHER_PARAS}

# modify bundle name in app profile, distribution-certificate in profile is ready
echo "modify bundle name in profile, and generate app profile ${SIGN_APP_DIR}/${MOD_PROFILE}"
jq ".\"bundle-info\".\"bundle-name\"=\"$BUNDLE_NAME\"" "${PROFILE_TEMPLATE}" > "${SIGN_APP_DIR}/${MOD_PROFILE}"

# sign profile, cert in profile can be ready for all apps
echo "sign the profile, and generate ${SIGN_APP_DIR}/${PROFILE_SIGNED_FILE}"
java -jar "${SCRIPT_PATH}/hap-sign-tool.jar" sign-profile -keyAlias "${PROFILE_KEY_ALIAS}" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "${SCRIPT_PATH}/${PROFILE_CERT_FILE}" -inFile "${SIGN_APP_DIR}/${MOD_PROFILE}" -keystoreFile "${SCRIPT_PATH}/${OH_KEY_FILE}" -outFile "${SIGN_APP_DIR}/${PROFILE_SIGNED_FILE}" -keyPwd "123456" -keystorePwd "123456"

# hap convert to bin
echo "convert ${SIGN_APP_DIR}/${NEW_HAP_FILE} to ${SIGN_APP_DIR}/${UNSIGNED_APP_NAME}"
java -jar "${SCRIPT_PATH}/haptobin_tool.jar" --hap-path "${SIGN_APP_DIR}/${NEW_HAP_FILE}" --bin-path "${SIGN_APP_DIR}/${UNSIGNED_APP_NAME}"

# generate signed bin 
echo "generate signed bin ${SIGN_APP_DIR}/${SIGNED_APP_NAME}"
java -jar "${SCRIPT_PATH}/hap-sign-tool.jar" sign-app -keyAlias "${APP_KEY_ALIAS}" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "${SCRIPT_PATH}/${APP_CERT_FILE}" -profileFile "${SIGN_APP_DIR}/${PROFILE_SIGNED_FILE}" -inFile "${SIGN_APP_DIR}/${UNSIGNED_APP_NAME}" -inForm bin -keystoreFile "${SCRIPT_PATH}/${OH_KEY_FILE}" -outFile "${SIGN_APP_DIR}/${SIGNED_APP_NAME}" -keyPwd "123456" -keystorePwd "123456"

# copy signed bin
echo "cp ${SIGN_APP_DIR}/${SIGNED_APP_NAME} to ${OUTPUT_HAP_PATH}/${SIGNED_APP_NAME}"
cp -f "${SIGN_APP_DIR}/${SIGNED_APP_NAME}" "${OUTPUT_HAP_PATH}/${SIGNED_APP_NAME}"

# clear
echo "remove dir ${SIGN_APP_DIR}"
rm -rf ${SIGN_APP_DIR}

exit 0